<template>
    <div class="home-news">
        <div class="page-center">
            <div class="news-section clearfix">
                <div class="left-section">
                    <div class="swiper-container swiper-home-news">
                        <div class="swiper-wrapper">
                            <div class="swiper-slide" v-for="(news, sIndex) in slides" :key="sIndex">
                                <div class="news-left" v-if="news[0]">
                                    <dd>
                                        <a :href="`/news-detail?id=${news[0].id}`">
                                            <img :src="news[0].imgUrl" :alt="news[0].title" />
                                            <p class="circle"><i class="fa fa-plus"></i></p>
                                        </a>
                                    </dd>
                                    <dd class="dd-title">
                                        <a :href="`/news-detail?id=${news[0].id}`">
                                            <h3 class="title">{{ news[0].title }}</h3></a
                                        >
                                    </dd>
                                    <dd class="dd-date"><i class="fa fa-calendar"></i>{{ news[0].date }}</dd>
                                </div>
                                <div class="news-right" v-if="news.length > 1">
                                    <div>
                                        <dd>
                                            <a :href="`/news-detail?id=${news[1].id}`">
                                                <img :src="news[1].imgUrl" :alt="news[1].title" />
                                                <p class="circle"><i class="fa fa-plus"></i></p>
                                            </a>
                                        </dd>
                                        <dd class="dd-title">
                                            <a :href="`/news-detail?id=${news[1].id}`">
                                                <h3 class="title">{{ news[1].title }}</h3></a
                                            >
                                        </dd>
                                    </div>
                                    <template v-if="news.length > 2">
                                        <div>
                                            <dd>
                                                <a :href="`/news-detail?id=${news[2].id}`">
                                                    <img :src="news[2].imgUrl" :alt="news[2].title" />
                                                    <p class="circle"><i class="fa fa-plus"></i></p>
                                                </a>
                                            </dd>
                                            <dd class="dd-title">
                                                <a :href="`/news-detail?id=${news[2].id}`">
                                                    <h3 class="title">{{ news[2].title }}</h3></a
                                                >
                                            </dd>
                                        </div>
                                    </template>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="right-section">
                    <div class="title clearfix">
                        <h2>活动、福利</h2>
                        <span></span>
                        <span></span>
                    </div>
                    <p class="desc">最新行业资讯、公司资讯、活动信息</p>
                    <div class="swiper-prev-next">
                        <div class="button" @click="priveSlide"><i class="fa fa-arrow-left"></i></div>
                        <div class="button" @click="nextSlide"><i class="fa fa-arrow-right"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import Swiper from 'swiper'
import 'swiper/dist/css/swiper.min.css'

// TODO 从接口获取
const data = [
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230310110204_1553.png',
        title: '盛夏时节，共赴生辰|菲斯生物第二季度生日会',
        id: 1,
        date: '2024-03-15',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714021400_5479.jpg',
        title: 'E-G04|苏州CMC-China，菲斯期待与您相聚！',
        id: 2,
        date: '2024-03-16',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714020015_8384.jpg',
        title: '抗流感新药-超级达菲-巴洛沙韦酯',
        id: 3,
        date: '2024-03-17',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714023247_7699.jpg',
        title: '2盛夏时节，共赴生辰|菲斯生物第二季度生日会',
        id: 4,
        date: '2024-03-18',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714021400_5479.jpg',
        title: '2E-G04|苏州CMC-China，菲斯期待与您相聚！',
        id: 5,
        date: '2024-03-19',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714020015_8384.jpg',
        title: '2抗流感新药-超级达菲-巴洛沙韦酯',
        id: 6,
        date: '2024-03-20',
    },
    {
        imgUrl: 'https://www.sincopharmachem.com.cn/Images/blog/230714020015_8384.jpg',
        title: '3抗流感新药-超级达菲-巴洛沙韦酯',
        id: 7,
        date: '2024-03-21',
    },
]

const chunkArray = (array, size) => {
    const chunkedArr = []
    for (let i = 0; i < array.length; i += size) {
        chunkedArr.push(array.slice(i, i + size))
    }
    return chunkedArr
}
const slides = chunkArray(data, 3)

const homeNewsSwiper = ref(null)

onMounted(() => {
    homeNewsSwiper.value = new Swiper('.swiper-home-news', {
        slidesPerView: 1,
        slidesPerGroup: 1,
        slidesBetween: 0,
    })
})

const priveSlide = () => {
    homeNewsSwiper.value.slidePrev()
}
const nextSlide = () => {
    homeNewsSwiper.value.slideNext()
}
</script>
<style lang="scss" scoped>
.home-news {
    background: #f7f7f7;
    padding: 30px 0;
}

dd a {
    display: inline-block;
    width: 100%;
}
dd img {
    max-width: 100%;
}
dd .circle {
    z-index: 9999;
    width: 40px;
    height: 40px;
    display: inline-block;
    text-align: center;
    line-height: 30px;
    background: #fff;
    color: #000;
    border: 5px solid #0d87dc;
    border-radius: 50px;
    position: absolute;
    bottom: -15px;
    right: 20px;
    i {
        font-size: 16px;
        font-weight: 600;
        transition: transform 1.5s ease;
    }
    &:hover {
        i {
            transform: rotate(360deg);
        }
    }
}
.dd-title {
    .title {
        color: #333;
        font-size: 16px;
    }
    &:hover {
        .title {
            color: #0d87dc;
        }
    }
}
.left-section {
    width: 75%;
    float: left;

    .news-left {
        width: 60%;
        float: left;

        dd {
            width: 100%;
            float: left;
            margin-bottom: 10px;
            position: relative;
            line-height: 180%;
        }
        .dd-date {
            color: #0d87dc;
            i {
                margin-right: 5px;
            }
        }
    }
    .news-right {
        width: 38%;
        float: right;

        dd {
            width: 100%;
            float: left;
            margin-bottom: 15px;
            position: relative;
            font-weight: bold;
        }
    }
}
.right-section {
    float: right;
    width: 25%;
    padding: 0 3%;
    margin-top: 30px;

    h2 {
        letter-spacing: 4px;
        color: #000;
        font-size: 32px;
        margin-bottom: 15px;
    }
    .title {
        span {
            width: 70px;
            float: left;
            display: inline-block;
            transition: all 0.5s;
            height: 5px;
            &:first-of-type {
                background: #0d87dc;
                height: 5px;
                margin-right: 2px;
            }
            &:last-child {
                background: #993b2f;
                height: 5px;
            }
        }
    }
    &:hover {
        .title {
            span {
                width: 50px;
            }
        }
    }
    .swiper-prev-next {
        .button {
            background: #efefef;
            height: 40px;
            width: 40px;
            border-radius: 100px;
            line-height: 40px;
            text-align: center;
            float: left;
            cursor: pointer;
            margin-right: 10px;
            &:hover {
                background: #0d87dc;
                color: #fff;
            }
        }
    }

    .desc {
        margin: 15px 0;
        font-size: 14px;
        color: #666;
    }
}
</style>
